package com.shqcjd.sinopecdemomobile.ui.activity.tipwizard;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.eventbus.RefreshData;
import com.shqcjd.sinopecdemomobile.model.pojo.AppOperationGuideMode;
import com.shqcjd.sinopecdemomobile.model.pojo.TreeNode;
import com.shqcjd.sinopecdemomobile.ui.activity.problem.ProblemFeedbackActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * 操作向导页面
 */
public class TipWizardWorkActivity extends BaseActivity {

    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_feedback_btn)
    TextView tvFeedbackBtn;
    @BindView(R.id.tv_video_btn)
    TextView tvVideoBtn;
    @BindView(R.id.tv_img_btn)
    TextView tvImgBtn;
    @BindView(R.id.tv_finish_btn)
    TextView tvFinishBtn;
    @BindView(R.id.button_description)
    TextView tvButtonDesc;
    @BindView(R.id.tv_info)
    TextView tv_info;

    private int stepIndexId;
    private int stepDetailId;

    private int nextId,prevId;

    private AppOperationGuideMode.ItemsBean node;
    private String kks;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_tip_wizard_work;
    }


    @OnClick({R.id.tv_feedback_btn, R.id.tv_video_btn, R.id.tv_img_btn, R.id.tv_finish_btn,R.id.tv_confirm,R.id.next_btn,R.id.prev_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_feedback_btn://意见反馈
                Intent it = new Intent(this, ProblemFeedbackActivity.class);
                it.putExtra("taskId", node.workTaskId+"");
                it.putExtra("kks", kks);
                it.putExtra("projectName", node.equipmentTypeName);
                startActivity(it);
                break;
            case R.id.tv_video_btn://视频
                if (stepDetailId!=0){
                    Intent intent = new Intent(this,TipWizardVideoActivity.class);
                    intent.putExtra("stepDetailId",stepDetailId);
                    startActivity(intent);
                }

                break;
            case R.id.tv_img_btn://图片
                if (stepDetailId!=0){
                    Intent intent = new Intent(this,TipWizardImageActivity.class);
                    intent.putExtra("stepDetailId",stepDetailId);
                    startActivity(intent);
                }

                break;
            case R.id.tv_finish_btn:
                finish();
                break;
            case R.id.tv_confirm:
                if (nextId==0){
                    ToastUtils.showShort(TipWizardWorkActivity.this,"已经是最后一步");
//                    taskComplete();
                    return;
                }
                nextStep();
            case R.id.next_btn:
                if (nextId==0){
                    ToastUtils.showShort(TipWizardWorkActivity.this,"已经是最后一步");
                    return;
                }
                nextStep();
                break;
            case R.id.prev_btn:
                if (prevId==0){
                    ToastUtils.showShort(TipWizardWorkActivity.this,"已经是第一步");
                    return;
                }
                prevStep();
                break;
        }
    }

    @Override
    public void initView() {
        super.initView();
        node = (AppOperationGuideMode.ItemsBean) getIntent().getSerializableExtra("data");
        stepIndexId = getIntent().getIntExtra("stepIndexId",-1);
        showWaitingDialog("正在获取数据");
        ApiService.getStepDetail(stepIndexId,node.workTaskId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<TreeNode.StepDetail>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<TreeNode.StepDetail> retData) {
                        if (retData.retCode == 10000 && retData.data != null) {
                            kks = retData.data.kks;
                            tvContent.setText(retData.data.step_detail_content);
                            tv_info.setText(retData.data.step_detail_standard);
                            stepDetailId = retData.data.id;
                            nextId = retData.data.nextId;
                            prevId = retData.data.prevId;
//                            if (nextId==0){
//                                tvButtonDesc.setText("结束");
//                            }
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }


    private void showFinishDialog(){
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .customView(R.layout.layout_work_finish, false)
                .show();
        dialog.findViewById(R.id.confirm_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                showGoodDialog();
            }
        });

        dialog.findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }


    private void showGoodDialog(){
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .customView(R.layout.layout_good, false)
                .show();
        dialog.findViewById(R.id.good_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                doGood();
            }
        });
        dialog.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                EventBus.getDefault().post(new RefreshData());
                finish();
            }
        });
        dialog.setCanceledOnTouchOutside(false);
    }

    private void doGood(){
        showWaitingDialog("正在获取数据");
        ApiService.doGood(node.equipmentTypeId,node.guideType)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<Integer>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<Integer> retData) {
                        if (retData.retCode == 10000 ) {
                            ToastUtils.showShort(TipWizardWorkActivity.this,"谢谢您的点赞！");
                            EventBus.getDefault().post(new RefreshData());
                            finish();
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putSerializable("data", node);
        savedInstanceState.putInt("stepDetailId", stepDetailId);

    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        node = (AppOperationGuideMode.ItemsBean) savedInstanceState.getSerializable("data");
        stepDetailId = savedInstanceState.getInt("stepDetailId");
    }

    private void taskComplete(){
        showWaitingDialog("正在获取数据");
        ApiService.taskComplete(node.workTaskId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<Integer>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<Integer> retData) {
                        if (retData.retCode == 10000 && retData.data == 1) {
                            showFinishDialog();
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }



    private void nextStep(){
        showWaitingDialog("正在获取数据");
        ApiService.getNextStepDetail(stepDetailId,node.workTaskId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<TreeNode.StepDetail>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<TreeNode.StepDetail> retData) {
                        if (retData.retCode == 10000 && retData.data != null) {
                            kks = retData.data.kks;
                            tvContent.setText(retData.data.step_detail_content);
                            tv_info.setText(retData.data.step_detail_standard);
                            stepDetailId = retData.data.id;
                            nextId = retData.data.nextId;
                            prevId = retData.data.prevId;
//                            if (nextId==0){
//                                tvButtonDesc.setText("结束");
//                            }
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    private void prevStep(){
        showWaitingDialog("正在获取数据");
        ApiService.getPrevStepDetail(stepDetailId, node.workTaskId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<TreeNode.StepDetail>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<TreeNode.StepDetail> retData) {
                        if (retData.retCode == 10000 && retData.data != null) {
                            kks = retData.data.kks;
                            tvContent.setText(retData.data.step_detail_content);
                            tv_info.setText(retData.data.step_detail_standard);
                            stepDetailId = retData.data.id;
                            nextId = retData.data.nextId;
                            prevId = retData.data.prevId;
//                            tvButtonDesc.setText("确定");
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }
}
